package Servlet;

import com.cyy.bean.*;
import com.cyy.dao.historyDao;
import com.cyy.dao.userDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/showEmail")
public class showEmailServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName=request.getSession().getAttribute("name").toString();
        ArrayList<record> records=null;
//        获得该用户未确认收获的已下单商品的id
        try {
            records=historyDao.selectNoConfirm(userName);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
//        获取他们的下单时间
        String time;
        ArrayList<String> times=new ArrayList<String>();
        times.add("");
        if(records!=null) {
            for (int i = 0; i < records.size(); i++)
            {
                time=records.get(i).getTime();
                if(!times.contains(time)) {
                    times.add(time);
                }
            }
        }
        times.remove("");
//        获取用户的基本信息
        userDao userDao = new userDao();
        user user=null;
        try {
            user=userDao.selectAll(userName);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        assert user != null;
        request.setAttribute("address",user.getAddress());
        request.setAttribute("phone",user.getPhone());
        request.setAttribute("records",records);
        request.setAttribute("times",times);
        request.getRequestDispatcher("customerEmail.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
